//
//  NetWorkTools.m
//  
//
//  Created by 张贞献 on 16/5/10.
//
//

#import "NetWorkTools.h"
#import "Reachability.h"

@implementation NetWorkTools

+(instancetype)shareManager {
    
//    static id instance;
     static NetWorkTools * manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
       //1. configuration 配置
        NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        //2. AFNSessionManager
        manager = [[NetWorkTools alloc] initWithSessionConfiguration:configuration];
        //3.返回类型
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        //4. 请求格式
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //5.超时
        manager.requestSerializer.timeoutInterval = 10.0;
        
        
    });
    
    return manager;
}

//检测网络
-(BOOL)isReachableByAFN {
    
    Reachability * reachability = [Reachability reachabilityWithHostName:CZ_BaseUrl];
    
    return reachability.isReachable;
}

-(void)GET_RequestWithUrlString:(NSString *)urlString parmas:(NSDictionary *)params successBloock:(SuccessCompletedBlock)success failureBlock:(FailureCompletedBlock)failure {
    
    [[NetWorkTools shareManager] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
    }];
    
}

-(void)POST_RequestWithUrlString:(NSString *)urlString parmas:(NSDictionary *)params successBloock:(SuccessCompletedBlock)success failureBlock:(FailureCompletedBlock)failure {
    
    [[NetWorkTools shareManager] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
    }];
}
@end
